// packageE/grab_group/grab_group_my/grab_group_my.js
var app = getApp();
// import Dialog from "../../../dist/dialog/dialog";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    language: "",
    dataList: [],
    numberRounds: 0,
    member: {},
    titleArr: ["抢团中", "抢团中奖", "抢团失败"],
    active: 0,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,

    showBarter: false,
    showBarter_value: null,
    orderPayId: null,
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let language = wx.getStorageSync("langIndex");
    this.setData({
      language: language.en,
    });
    this.getdata();
  },
  handleClick(e) {
    this.setData({
      dataList: [],
      active: e.detail.index,
    });
    this.getdata();
  },
  getdata() {
    let that = this;
    let urlStr = app.getNetAddresss(
      "plugin.snatch-regiment.api.index.getSnatchData"
    );
    app._getNetWork({
      url: urlStr,
      data: {
        art: "myTeam",
        state: that.data.active + 1,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            isLoadMore: true,
            page: 1,
            total_page: res.data.myTeam.list.last_page,
            dataList: res.data.myTeam.list.data,
            member: res.data.myTeam.member,
            numberRounds: res.data.myTeam.numberRounds,
          });
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  gotoRecord() {
    wx.navigateTo({
      url: "/packageE/grab_group/grab_group_my_record/grab_group_my_record",
    });
  },
  gotoOrderDetail(e) {
    let order_id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url:
        "/packageA/member/orderdetail/orderdetail?order_id=" +
        order_id +
        "&orderType=grabGroup",
    });
  },
  gotoGroup(e) {
    let id = e.target.dataset.id || e.currentTarget.dataset.id;
    wx.navigateTo({
      url:
        "/packageE/grab_group/grab_group_detail/grab_group_detail?" +
        "&id=" +
        id,
    });
  },
  //根据按钮值判断用途
  operation(e) {
    let btn = e.currentTarget.dataset.data;
    let barter = e.currentTarget.dataset.barter;
    let order = {
      id: e.currentTarget.dataset.orderid,
    };
    this.setData({
      orderPayId: order.id,
    });
    if (btn.value == 8 || btn.value == "snatch_regiment_express") {
      this.checklogistics(order, btn);
    } else if (btn.value == 10) {
      this.comment(order, btn);
    } else if (btn.value == 1 || btn.value == "snatch_regiment_pay") {
      this.toPay(order, btn);
    } else if (btn.value == 13 || btn.value == "snatch_regiment_refund") {
      //退款
      this.getTemplate(order, btn);
    } else if (btn.value == 18) {
      //退款中
      this.toRefundDetail(order, btn);
    } else if (btn.value == 3) {
      this.gotoRefund(order.id);
    } else if (btn.value == 40) {
      this.gotoRefundCheck(order.id);
    } else if (btn.value == "snatch_regiment_barter") {
      // 抢团易货兑换
      this.setData({
        showBarter: true,
        showBarter_value: barter,
      });
    } else {
      this.publicMessage(btn, order);
    }
  },
  confirmBarter() {
    let urlStr = app.getNetAddresss("plugin.snatch-regiment.api.order.barter");
    urlStr += "&order_id=" + this.data.orderPayId;
    app._getNetWork({
      url: urlStr,
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          wx.showToast({
            title: `易货兑换成功`,
            icon: "success",
            duration: 1500,
            success: function () {},
          });
          that.getdata();
        } else {
          wx.showToast({
            icon: "none",
            title: res.msg,
            duration: 1200,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  cancelBarter() {
    this.setData({
      showBarter: false,
      showBarter_value: null,
    });
  },
  //按钮公共方法，点击提示框进行操作，确认后刷新
  publicMessage(btn, order) {
    console.log(btn);
    let that = this;
    if (!btn.api) {
      return;
    }
    wx.showModal({
      title: "提示",
      content: `确定${btn.name}?`,
      success(res) {
        if (res.confirm) {
          let urlStr = "";
          urlStr += app.getNetAddresss(btn.api);
          urlStr += "&order_id=" + order.id;
          app._getNetWork({
            url: urlStr,
            success: function (resdata) {
              var res = resdata.data;
              if (res.result == 1) {
                that.getdata();
                wx.showToast({
                  title: `${btn.name}成功`,
                  icon: "success",
                  duration: 1500,
                  success: function () {
                    // setTimeout(function() {
                    // 	//要延时执行的代码
                    // 	wx.navigateBack({
                    // 		delta: 1
                    // 	})
                    // }, 1500) //延迟时间
                  },
                });
              } else {
                wx.showToast({
                  icon: "none",
                  title: res.msg,
                  duration: 1000,
                });
              }
            },
            fail: function (res) {
              console.log(res);
            },
          });
        }
      },
    });
  },
  //查看物流
  checklogistics(item, btn) {
    wx.navigateTo({
      url:
        "/packageA/member/order/logistics/logistics?id=" +
        item.id +
        "&api=" +
        btn.api,
    });
  },
  //评价订单
  comment(item, btn) {
    // 跳到评价订单
    wx.navigateTo({
      url:
        "/packageB/member/comment/comment?order=" +
        JSON.stringify(item ? item : {}),
    });
  },
  toPay(item, btn) {
    //status-2合并支付 type-1订单支付
    wx.navigateTo({
      url:
        "/packageD/buy/orderPay/orderPay?order_id=" +
        item.id +
        "&status=2" +
        "&api=" +
        btn.api,
    });
  },
  //获取模板id
  getTemplate(order, btn) {
    let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
    let that = this;
    app._getNetWork({
      url: urlStr,
      data: {
        small_type: 2,
      },
      success: function (resdata) {
        var res = resdata.data;
        if (res.result == 1) {
          let array = [];
          for (let i = 0; i < res.data.length; i++) {
            array.push(res.data[i].template_id);
          }
          that.setData({
            TemplateArray: array,
          });
          wx.requestSubscribeMessage({
            tmplIds: that.data.TemplateArray,
            success(res) {
              that._toRefund(order, btn);
            },
            fail() {
              that._toRefund(order, btn);
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1500,
          });
        }
      },
      fail: function (res) {
        console.log(res);
      },
    });
  },
  //退款
  _toRefund(item, btn) {
    //refund
    wx.navigateTo({
      url:
        "/packageD/member/myOrder/refund/refund?order_id=" +
        item.id +
        "&type=3",
    });
  },
  //退款中
  toRefundDetail(item, btn) {
    //aftersales
    wx.navigateTo({
      url:
        "packageD/member/myOrder/Aftersales/Aftersales?refund_id=" +
        item.refund_id,
    });
  },
  //退还跳转
  gotoRefund(val) {
    wx.navigateTo({
      url: "/packageB/member/OrderReturn/OrderReturn?id=" + val,
    });
  },
  //退还跳转
  gotoRefundCheck(val) {
    wx.navigateTo({
      url: "/packageB/member/OrderReturnCheck/OrderReturnCheck?id=" + val,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      this._getMoreData();
    } else {
      console.log("没有更多数据");
    }
  },
  //获取更多数据
  _getMoreData() {
    const that = this;
    let json = {};
    let api = "";
    let urlStr = "";
    that.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      api = "plugin.snatch-regiment.api.index.getSnatchData";
      json = {
        page: that.data.page,
        art: "myTeam",
        state: that.data.active + 1,
      };

      urlStr = app.getNetAddresss(api);
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: json,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              isLoadMore: true,
              dataList: that.data.dataList.concat(res.data.myTeam.list.data),
            });
          } else {
            that.setData({
              page: that.data.page - 1,
              isLoadMore: false,
            });
            return;
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
